﻿using System;
using System.Collections.Generic;
using System.Text;
using KaiXin001er.Objects;
using KaiXin001er.WebBrowser;
using KaiXin001er.UTIL;
using System.Web;
using System.Net.Json;
using System.Drawing;
using System.Windows.Forms;

namespace KaiXin001er.Logic
{
    public class FarmPlayer : BasePlayer
    {
        public bool 偷盗次数到头了 = false;
        public FarmPlayer(Account acct) : base(acct) { }

        public override bool SignOn()
        {
            WebAccessor dac = WebAccessor.Instance;
            string ret = dac.GetRequest(URLS.房子首页, "");
            string money = StringHelper.getMiddleString("现金：<span style=\"color:#D01E3B;width:auto;\"><strong style=\"font-family:Arial;font-size:12px;\" title=\"", "元\"", ret);
            RaiseMsg("[买房子模块] 现金 " + money + " 元");
            return true;
        }


        public override bool SignOff()
        {

            return true;
        }

        public override bool PlayPlay()
        {
            偷盗次数到头了 = false;
            //分四部
            //1   收获花园
            if (Account.IsGarden) PlayAsGardener();
            //3 收获牧场
            if (Account.IsRancher) PlayAsRancher();
            //5打工
            if (Account.IsDoJob) PlayAsEmpl();
            return true;
        }

        private void PlayAsGardener()
        {
            //先收获自己的 
            string gardenIndex = dac.SimpleRequest(URLS.花园菜地);
            string verify = 页面分析者.花园verify(gardenIndex);
            请求参数 r = new 请求参数();
            r.add("fuid", "0");
            r.add("r", RandomGen.随机小数());
            r.add("verify", verify);
            string config = dac.GetRequest(URLS.花园菜地conf, r.ToString());
            GardenUserInfo g = 页面分析者.花园用户信息(config);
            RaiseMsg("[花园菜地模块] " + g.Name + g.Ranktip + g.Cashtip + "魅力" + g.Tcharms);
            foreach (GardenItem gi in g.GardenItem)
            {
                if (gi.Cropsstatus == "1" || gi.Cropsstatus == "2" || gi.Cropsstatus == "-1")
                    RaiseMsg("[花园菜地模块]  菜地" + gi.Farmnum + ":" + gi.Name + gi.Crops + "|" + gi.Farm);
            }
            foreach (GardenItem gi in g.GardenItem)
            {
                //浇水
                ///暂时没看到
                if (gi.Water == "0")
                {
                    gi.Water = "5";
                }
            }
            捉虫(verify, "0", g);
            除草(verify, "0", g);
            收获(verify, "0", g);

            //foreach (GardenItem gi in g.GardenItem)
            //{
            //    //收获//偷
            //    if (gi.Cropsstatus == "2")
            //    {
            //        请求参数 harvestr = new 请求参数();
            //        harvestr.add("farmnum", gi.Farmnum);
            //        harvestr.add("verify", verify);
            //        harvestr.add("r", RandomGen.随机小数());
            //        harvestr.add("fuid", "0");
            //        harvestr.add("seedid", "0");
            //        string harveststring = dac.GetRequest(URLS.花园收获和偷, harvestr.ToString());
            //        if (harveststring.Contains("succ"))
            //        {
            //            int pos = 0;
            //            RaiseMsg("[花园菜地模块] 收获");
            //            gi.Cropsstatus = "3";//设置成3好锄地
            //            //string stealnum = StringHelper.ReadXmlSchema("stealnum", harveststring);
            //            //string seedname = StringHelper.ReadXmlSchema("seedname", harveststring);
            //            //RaiseMsg("[花园菜地模块] 收获" + stealnum + "个" + seedname);
            //        }
            //        else
            //        {
            //            int pos = 0;
            //            string Reason = StringHelper.ReadXmlSchema("reason", harveststring, ref pos);
            //            RaiseMsg("[花园菜地模块] " + Reason);
            //        }
            //    }
            //}
            锄地(verify, g);

            //再偷别人的
            //一次来到好友的花园中一次除草和偷
            string tuid = StringHelper.getMiddleString("var g_nextuid = \"", "\";", gardenIndex);
            while (tuid != this.Account.PlayerID)
            {//一直往next邻居走

                string fuid = tuid;
                请求参数 checkr = new 请求参数();
                checkr.add("touid", fuid);
                checkr.add("verify", verify);
                string checkString = dac.PostRequest(URLS.花园菜地check, r.ToString());//暂时该页没什么用处吧
                Friend f = this.FindFriendByID(fuid);
                RaiseMsg("[花园菜地模块] 来到" + f.FriendName + "的菜地");
                请求参数 friendNextr = new 请求参数();
                friendNextr.add("fuid", fuid);
                friendNextr.add("r", RandomGen.随机整数());
                friendNextr.add("verify", verify);
                string friendAjax = dac.PostRequest(URLS.花园friendNext, friendNextr.ToString());
                JsonTextParser jsontextparser = new JsonTextParser();
                JsonObjectCollection joc = jsontextparser.Parse(friendAjax) as JsonObjectCollection;
                tuid = joc["nextuid"].GetValue() as string;
                //tuid = StringHelper.getMiddleString("\"nextuid\":\"", "", "", friendAjax);
                请求参数 friendconfigr = new 请求参数();
                friendconfigr.add("fuid", fuid);
                friendconfigr.add("r", RandomGen.随机小数());
                friendconfigr.add("verify", verify);
                string friendConfig = dac.GetRequest(URLS.花园菜地conf, friendconfigr.ToString());
                GardenUserInfo gui = 页面分析者.花园用户信息(friendConfig);
                RaiseMsg("[花园菜地模块] " + gui.Name + gui.Ranktip + gui.Cashtip + "魅力" + gui.Tcharms);
                foreach (GardenItem gi in gui.GardenItem)
                {
                    if (gi.Cropsstatus == "1" || gi.Cropsstatus == "2" || gi.Cropsstatus == "-1")
                        RaiseMsg("[花园菜地模块]  菜地" + gi.Farmnum + ":" + gi.Name + gi.Crops + "|" + gi.Farm);
                }
                foreach (GardenItem gi in gui.GardenItem)
                {
                    //浇水
                    ///暂时没看到
                    if (gi.Water == "0")
                    {
                        gi.Water = "5";
                    }
                }
                if (Account.IsAntivirmin) 捉虫(verify, fuid, gui);
                if (Account.IsAntiGrass) 除草(verify, fuid, gui);

                if (!偷盗次数到头了)
                {
                    if (Account.IsGardenSteal) 收获(verify, fuid, gui);
                }

            }
        }

        private void 锄地(string verify, GardenUserInfo g)
        {
            foreach (GardenItem gi in g.GardenItem)
            {
                if (gi.Cropsstatus == "3" || gi.Cropsstatus == "-1")
                {
                    // 该犁地了
                    请求参数 lidir = new 请求参数();
                    lidir.add("farmnum", gi.Farmnum);
                    lidir.add("fuid", "0");
                    lidir.add("r", RandomGen.随机小数());
                    lidir.add("seedid", "0");
                    lidir.add("verify", verify);
                    string lidistring = dac.GetRequest(URLS.花园锄地, lidir.ToString());
                    if (lidistring.Contains("succ"))
                    {
                        RaiseMsg("[花园菜地模块] " + gi.Farmnum + "锄地成功");
                    }
                    else { }
                }
            }
        }

        private void 收获(string verify, string fuid, GardenUserInfo gui)
        {
            if (偷盗次数到头了) return;
            foreach (GardenItem gi in gui.GardenItem)
            {
                //收获//偷
                if (gi.Cropsstatus == "2" && gi.Shared != "1")
                {
                    ///跳过已经偷过的
                    if (gi.Crops.Contains("已偷过"))
                    {
                        RaiseMsg("[花园菜地模块] " + gi.Name + "已经偷过了,做人厚道点吧");
                    }
                    ///跳过剩下一个的哪些
                    if (最后一个不能偷的作物.contains(gi.Name) && fuid != "0")//不是自己的时候起作用
                    {
                        if (StringHelper.getMiddleString("剩余：", "&lt;br", gi.Crops) == "1")
                        {
                            RaiseMsg("[花园菜地模块] 手下留情，" + gi.Name + "作物最后一个不能偷");
                            continue;
                        }
                    }
                    //可以偷
                    请求参数 harvestr = new 请求参数();
                    harvestr.add("farmnum", gi.Farmnum);
                    harvestr.add("verify", verify);
                    harvestr.add("r", RandomGen.随机小数());
                    harvestr.add("fuid", fuid);
                    harvestr.add("seedid", "0");

                    string harveststring = dac.GetRequest(URLS.花园收获和偷, harvestr.ToString());
                    if (harveststring.Contains("succ"))
                    {
                        if ((StringHelper.ReadXmlSchema("anti", harveststring)) == "1")
                        {
                            RaiseMsg("[花园菜地模块] 遇见花园精灵了");
                            请求参数 smartcr = new 请求参数();
                            smartcr.add("verify", verify);
                            smartcr.add("fuid", "0");
                            smartcr.add("r", RandomGen.随机小数());
                            string FruitString = dac.GetRequest(URLS.花园精灵水果图片, smartcr.ToString());
                            SmartC sc = 页面分析者.花园精灵信息(FruitString);
                            请求参数 smartqr = new 请求参数();
                            smartqr.add("verify", verify);
                            smartqr.add("r", RandomGen.随机小数());
                            Image smartQimage = dac.GetImage(sc.Tpic, smartqr.ToString());
                            FrmSmartAnswer fsa = new FrmSmartAnswer();
                            fsa.setData(sc, smartQimage);
                            if (fsa.ShowDialog() == DialogResult.OK)
                            {
                                请求参数 answerr = new 请求参数();
                                answerr.add("verify", verify);
                                answerr.add("ordernum", fsa.AnswerString);
                                string answerRet = dac.GetRequest(URLS.花园精灵回答, answerr.ToString());
                                if (answerRet.Contains("succ"))
                                {
                                    请求参数 ginsengr = new 请求参数();
                                    ginsengr.add("verify", verify);
                                    ginsengr.add("r", RandomGen.随机小数());
                                    string ginsengRet = dac.GetRequest(URLS.花园精灵ginseng, ginsengr.ToString());
                                    //暂时没有任何作用
                                }
                            }

                        }
                        else
                        {
                            int pos = 0;
                            string stealnum = StringHelper.ReadXmlSchema("stealnum", harveststring, ref pos);
                            string harvestnum = StringHelper.ReadXmlSchema("num", harveststring);
                            string seedname = StringHelper.ReadXmlSchema("seedname", harveststring, ref pos);
                            if (fuid != "0") RaiseMsg("[花园菜地模块] 偷到" + stealnum + "个" + seedname);
                            else
                            {
                                RaiseMsg("[花园菜地模块] 收获" + harvestnum + "个" + seedname);
                            }
                        }
                    }
                    else
                    {
                        int pos = 0;
                        string Reason = StringHelper.ReadXmlSchema("reason", harveststring, ref pos);
                        if (Reason.Contains("今天偷果实的次数已用完")) { 偷盗次数到头了 = true; return; }//这是直接退出的条件
                        RaiseMsg("[花园菜地模块] 菜地" + gi.Farmnum + "号" + Reason);
                    }
                }
            }
        }

        private void 除草(string verify, string fuid, GardenUserInfo gui)
        {
            foreach (GardenItem gi in gui.GardenItem)
            {
                //除草
                if (gi.Grass != "0")
                {
                    请求参数 antigrassr = new 请求参数();
                    antigrassr.add("farmnum", gi.Farmnum);
                    antigrassr.add("fuid", fuid);
                    antigrassr.add("r", RandomGen.随机小数());
                    antigrassr.add("seedid", "0");
                    antigrassr.add("verify", verify);
                    string antiGrass = dac.GetRequest(URLS.花园除草, antigrassr.ToString());
                    if (antiGrass.Contains("succ")) RaiseMsg("[花园菜地模块] 第" + gi.Farmnum + "块儿菜地除草成功!");

                }
            }
        }

        private void 捉虫(string verify, string fuid, GardenUserInfo gui)
        {
            foreach (GardenItem gi in gui.GardenItem)
            {
                //捉虫
                if (gi.Vermin != "0")
                {
                    请求参数 antigrassr = new 请求参数();
                    antigrassr.add("farmnum", gi.Farmnum);
                    antigrassr.add("fuid", fuid);
                    antigrassr.add("r", RandomGen.随机小数());
                    antigrassr.add("seedid", "0");
                    antigrassr.add("verify", verify);
                    string antiGrass = dac.GetRequest(URLS.花园捉虫, antigrassr.ToString());
                    if (antiGrass.Contains("succ")) RaiseMsg("[花园菜地模块] 第" + gi.Farmnum + "块儿菜地捉虫成功!");
                }
            }
        }

        private void PlayAsRancher()
        {
            string ranchIndex = dac.GetRequest(URLS.花园牧场, "");
            string verify = StringHelper.getMiddleString("var g_verify = \"", "\";", ranchIndex);
            string fuid = "0";
            请求参数 r = new 请求参数();
            r.add("fuid", fuid);
            r.add("r", RandomGen.随机小数());
            r.add("verify", verify);
            string config = dac.GetRequest(URLS.花园牧场conf, r.ToString());
            RanchUserInfo ranchUserInfo = 页面分析者.牧场用户信息(config);
            foreach (RanchAnimal ra in ranchUserInfo.RanchAnimals)
            {

                RaiseMsg("[花园牧场模块]  动物" + ra.Aname + ":" + ra.Tips);
            }
            foreach (RanchProduct2 rpr in ranchUserInfo.RanchProducts)
            {
                RaiseMsg("[花园牧场模块]  产品" + rpr.Pname + ":" + rpr.Tips);
            }
            foreach (RanchFood rf in ranchUserInfo.RanchFoods)
            {
                RaiseMsg("[花园牧场模块] 食物" + rf.Tips);
            }
            RaiseMsg("[花园牧场模块] " + ranchUserInfo.Grasstips);
            RaiseMsg("[花园牧场模块] " + ranchUserInfo.Watertips);

            if (int.Parse(ranchUserInfo.Water) < 50)//需要添加水 少于50的话就天水
            {
                请求参数 waterr = new 请求参数();
                waterr.add("id", "0");
                waterr.add("foodnum", "1");
                waterr.add("seedid", "0");
                waterr.add("fuid", fuid);
                waterr.add("r", RandomGen.随机小数());
                waterr.add("verify", verify);
                waterr.add("type", "0");
                waterr.add("skey", "");
                string waterstring = dac.PostRequest(URLS.花园添水, waterr.ToString());
                if (waterstring.Contains("succ"))
                {
                    RaiseMsg("[花园牧场模块] 添水成功");
                }
            }
            //不添加牧草
            foreach (RanchAnimal ra in ranchUserInfo.RanchAnimals)
            {
                if (ra.Status == "1")//待产 赶一下 还没有看到那个状态可以使用
                {
                    请求参数 prodr = new 请求参数();
                    prodr.add("animalsid", ra.Animalsid);
                    prodr.add("fuid", fuid);
                    prodr.add("r", RandomGen.随机小数());
                    prodr.add("verify", verify);
                    string prodret = dac.GetRequest(URLS.花园牧场生产, prodr.ToString());
                    string ptips = StringHelper.ReadXmlSchema("ptips", prodret);
                    if (ptips != null) RaiseMsg("[花园牧场模块] " + ptips);

                }
                else if (ra.Status == "2")//收获
                {
                    请求参数 harvestr = new 请求参数();
                    harvestr.add("animalsid", ra.Animalsid);
                    harvestr.add("fuid", fuid);
                    harvestr.add("r", RandomGen.随机小数());
                    harvestr.add("verify", verify);
                    string harvestret = dac.GetRequest(URLS.花园牧场肉食收获, harvestr.ToString());
                    if (harvestret.Contains("succ"))
                    {
                        RaiseMsg("[花园牧场模块]  收获" + ra.Aname);
                    }
                }
            }
            foreach (RanchProduct2 rp in ranchUserInfo.RanchProducts)
            {
                ///一次收获产品
                ///
                请求参数 rpr = new 请求参数();
                rpr.add("id", "0");
                rpr.add("foodnum", "1");
                rpr.add("seedid", "0");
                rpr.add("fuid", fuid);
                rpr.add("r", RandomGen.随机小数());
                rpr.add("verify", verify);
                rpr.add("type", rp.Type);
                rpr.add("skey", rp.Skey);
                string harvestret = dac.GetRequest(URLS.花园牧场产品收获, rpr.ToString());
                if (harvestret.Contains("succ"))
                {
                    string num = StringHelper.ReadXmlSchema("num", harvestret);
                    RaiseMsg("[花园牧场模块]  收获" + num + "个" + rp.Pname);
                }
            }
            //不给添加牧草

            //再偷别人的
            //一次来到好友的花园中一次添水和偷
            string tuid = StringHelper.getMiddleString("var g_nextuid = \"", "\";", ranchIndex);
            while (tuid != this.Account.PlayerID)
            {//一直往next邻居走
                fuid = tuid;
                请求参数 friendNextr = new 请求参数();
                friendNextr.add("fuid", fuid);
                friendNextr.add("r", RandomGen.随机整数());
                friendNextr.add("verify", verify);
                string checkString = dac.GetRequest(URLS.花园friendNext, friendNextr.ToString());//暂时该页没什么用处吧
                string friendAjax = dac.PostRequest(URLS.花园friendNext, friendNextr.ToString());
                JsonTextParser jsontextparser = new JsonTextParser();
                JsonObjectCollection joc = jsontextparser.Parse(friendAjax) as JsonObjectCollection;
                tuid = joc["nextuid"].GetValue() as string;
                Friend f = this.FindFriendByID(fuid);
                RaiseMsg("[花园牧场模块] 来到" + f.FriendName + "的牧场");
                请求参数 configr = new 请求参数();
                configr.add("fuid", fuid);
                configr.add("r", RandomGen.随机小数());
                configr.add("verify", verify);
                string configf = dac.GetRequest(URLS.花园牧场conf, configr.ToString());
                RanchUserInfo ranchUserInfof = 页面分析者.牧场用户信息(configf);
                foreach (RanchAnimal ra in ranchUserInfof.RanchAnimals)
                {

                    RaiseMsg("[花园牧场模块]  动物" + ra.Aname + ":" + ra.Tips);
                }
                foreach (RanchProduct2 rpr in ranchUserInfof.RanchProducts)
                {
                    RaiseMsg("[花园牧场模块]  产品" + rpr.Pname + ":" + rpr.Tips);
                }
                foreach (RanchFood rf in ranchUserInfof.RanchFoods)
                {
                    RaiseMsg("[花园牧场模块] 食物" + rf.Tips);
                }
                RaiseMsg("[花园牧场模块] " + ranchUserInfo.Grasstips);
                RaiseMsg("[花园牧场模块] " + ranchUserInfo.Watertips);

                if (int.Parse(ranchUserInfof.Water) < 50 && Account.IsRancherWater)//需要添加水 少于50的话就天水
                {
                    请求参数 waterr = new 请求参数();
                    waterr.add("id", "0");
                    waterr.add("foodnum", "1");
                    waterr.add("seedid", "0");
                    waterr.add("fuid", fuid);
                    waterr.add("r", RandomGen.随机小数());
                    waterr.add("verify", verify);
                    waterr.add("type", "0");
                    waterr.add("skey", "");
                    string waterstring = dac.PostRequest(URLS.花园添水, waterr.ToString());
                    if (waterstring.Contains("succ"))
                    {
                        RaiseMsg("[花园牧场模块] 添水成功");
                    }
                }
                //不添加牧草

                //帮助生产
                if(Account.IsHelpProd)
                foreach (RanchAnimal ra in ranchUserInfof.RanchAnimals)
                {
                    if (ra.Tips.Contains("待产"))//待产 赶一下 暂时按照字符判断吧 还没有看到那个状态可以使用
                    {
                        请求参数 prodr = new 请求参数();
                        prodr.add("animalsid", ra.Animalsid);
                        prodr.add("fuid", fuid);
                        prodr.add("r", RandomGen.随机小数());
                        prodr.add("verify", verify);
                        string prodret = dac.GetRequest(URLS.花园牧场生产, prodr.ToString());
                        string ptips = StringHelper.ReadXmlSchema("ptips", prodret);
                        if (ptips != null) RaiseMsg("[花园牧场模块] " + ptips);

                    }
                }

                if(Account.IsRancherSteal)
                foreach (RanchProduct2 rp in ranchUserInfof.RanchProducts)
                {
                    ///一次收获产品
                    ///
                    请求参数 rpr = new 请求参数();
                    rpr.add("id", "0");
                    rpr.add("foodnum", "1");
                    rpr.add("seedid", "0");
                    rpr.add("fuid", fuid);
                    rpr.add("r", RandomGen.随机小数());
                    rpr.add("verify", verify);
                    rpr.add("type", rp.Type);
                    rpr.add("skey", rp.Skey);
                    string harvestret = dac.GetRequest(URLS.花园牧场产品收获, rpr.ToString());
                    if (harvestret.Contains("succ"))
                    {
                        string num = StringHelper.ReadXmlSchema("num", harvestret);
                        RaiseMsg("[花园牧场模块]  偷到" + num + "个" + rp.Pname);
                    }
                }

            }
        }

        private void PlayAsEmpl()
        {
            string joblistString = dac.GetRequest(URLS.打工列表, "");
            string verify = StringHelper.getMiddleString("g_verify = \"", "\";", joblistString);
            if (joblistString.Contains("别累坏了"))
            {
                ///
                string msg = StringHelper.getMiddleString("<div class=\"f14 l \" style=\"margin:20px 20px;width:26em;\">", "</div>", joblistString);
                RaiseMsg("[打工模块] " + msg);
            }
            else
            {
                请求参数 dojobr = new 请求参数();
                dojobr.add("verify", verify);
                string thebestJob = 页面分析者.最好的工作(joblistString);
                dojobr.add("petid", thebestJob);
                string dojobstr = dac.GetRequest(URLS.打工, dojobr.ToString());
                RaiseMsg("[打工模块] 选择" + thebestJob + "号工作");
                int flagpos = 0;
                StringHelper.seek(dojobstr, "parent.dojob_ret(", ref flagpos);
                string flag = StringHelper.getMiddleString(",", ")", dojobstr, ref flagpos);
                请求参数 dojobretr = new 请求参数();
                dojobretr.add("verify", verify);
                dojobretr.add("petid", thebestJob);
                string dojobRetstring = dac.GetRequest(URLS.打工结果, dojobretr.ToString());
                dojobretr.add("flag", flag);
                if (flag == "0")
                {
                    PlayAsEmpl();
                }
                else
                {
                    RaiseMsg("[打工模块] 已经在打工了");
                }
            }
        }



    }
}
